package jacob.service;

import jacob.bo.Article;
import jacob.bo.ArticleExample;
import jacob.dao.ArticleMapper;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by zjl on 2017/12/30.
 */
@Service
public class ArticleService {

    private static Logger logger = LoggerFactory.getLogger(ArticleService.class);

    @Autowired
    private ArticleMapper articleMapper;

    public void save(Article article){

        ArticleExample example = new ArticleExample();
        example.createCriteria().andTitleEqualTo(article.getTitle());
        List<Article> articles = articleMapper.selectByExample(example);
        if(CollectionUtils.isNotEmpty(articles)){
            logger.info(String.format("titile %s is exist id %d",article.getTitle(),articles.get(0).getId()));
            return;
        }
        articleMapper.insertSelective(article);
    }

    public List<Article> getAllarticle(){
        ArticleExample example = new ArticleExample();
        example.createCriteria().andIdGreaterThan(-1);
        example.setOrderByClause("publicTime");
        List<Article> articles = articleMapper.selectByExample(example);
        return articles;
    }
}
